################################################################
# Dynamic DiD: Visas Haiti and Crime Concerns and MRP outcomes
################################################################

rm(list=ls())

library(Hmisc)
library(readstata13)
library(foreign)
library(tidyverse)
library(did)
library(ggplot2)
set.seed(1111)

#sink("18_results_concerns_barp.txt")

############################
# Read and prepare data 
############################

# load data
load("final_county_2023april6.RData")
names(d_county)

#######################
# Results 
#######################

# Haiti
out1 <- att_gt(yname = "barp_s",
               gname = "first.treat_haiti",
               idname = "county",
               tname = "survey",
               data = d_county)

es1 <- aggte(out1, type = "dynamic",na.rm = TRUE)
pe1 <- es1$overall.att
se1 <- es1$overall.se
t1 = pe1/se1
pv1 = 2*pnorm(q=t1, lower.tail=F)

# All
out2 <- att_gt(yname = "barp_s",
               gname = "first.treat_all",
               idname = "county",
               tname = "survey",
               data = d_county)

es2 <- aggte(out2, type = "dynamic",na.rm = TRUE)
pe2 <- es2$overall.att
se2 <- es2$overall.se
t2 = pe2/se2
pv2 = 2*pnorm(q=t2, lower.tail=F)


#######################
# Table
#######################

# Visas from Haiti
pe1 = round(pe1,3)
se1 = round(se1,3) 
pv1 # p-value significant at 0.1

# Visas from All
pe2 = round(pe2,3)
se2 = round(se2,3)
pv2 

columns = c("visas from Haiti","","visas from all countries","","covariates","observations")
concerns = c(pe1,se1,pe2,se2,"no","1,348")
results = cbind(columns,concerns)
results

# Table a10
write.table(results, "tableA10.txt", sep="\t")

sink()
